package com.ruoyi.work.service.impl;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.work.domain.WorkFill;
import com.ruoyi.work.domain.enums.CheckType;
import com.ruoyi.work.mapper.WorkCheckMapper;
import com.ruoyi.work.service.IWorkCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author: MMDZ
 * @Desc:
 */
@Service
public class WorkCheckServiceImpl implements IWorkCheckService {

    @Autowired
    private WorkCheckMapper workCheckMapper;

    /**
     * 查询个人的审核列表
     *
     * @param fill 查询条件
     * @return
     */
    @Override
    public List<WorkFill> selectCheckListByUserId(WorkFill fill) {
        Long userId = SecurityUtils.getUserId();
        return workCheckMapper.selectCheckListByUserId(userId, fill.getNickName());
    }

    /**
     * 审核通过
     *
     * @param fill
     * @return
     */
    @Override
    public int updateStatePass(WorkFill fill) {
        if (Objects.isNull(fill.getFillId()) || Objects.isNull(fill.getFillState()))
            throw new ServiceException("申报数据异常，请联系管理员", HttpStatus.ERROR);
        fill.setFillState(CheckType.Checked.getCode()); // 通过
        fill.setUpdateBy(SecurityUtils.getUsername());
        fill.setCheckBy(SecurityUtils.getNickname());
        return workCheckMapper.updateState(fill);
    }

    /**
     * 批量 审核通过
     *
     * @param fills
     * @return
     */
    @Transactional
    @Override
    public int updateBatchStatePass(List<WorkFill> fills) {
        for (WorkFill fill : fills) {
            updateStatePass(fill);
        }
        return 1;
    }

    /**
     * 审核不通过，回退
     *
     * @param fill
     * @return
     */
    @Override
    public int updateStateRollback(WorkFill fill) {
        if (Objects.isNull(fill.getFillId()) || Objects.isNull(fill.getFillState()))
            throw new ServiceException("申报数据异常，请联系管理员", HttpStatus.ERROR);
        fill.setFillState(CheckType.Rollback.getCode()); // 不通过，回退
        fill.setUpdateBy(SecurityUtils.getUsername());
        fill.setCheckBy(SecurityUtils.getNickname());
        return workCheckMapper.updateState(fill);
    }
}
